package com.easyexcle.demo.pdf;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

/**
 * @Author: lkz
 * @Title: Base64
 * @Description: TODO
 * @Date: 2022/12/28 9:21
 * https://blog.csdn.net/qq_37469931/article/details/125235835
 */

public class Base64Utils {

    /***
     * 将图片转换为Base64<br>
     * 将base64编码字符串解码成img图片
     *
     * @param imgFile 文件地址
     * @return base64字符串
     * @author szs
     * @date 2020-05-05
     */
    public static String getBase64FromHttp(String imgFile) {
        if (!StrUtil.isNotBlank(imgFile)) {
            return null;
        }

        ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {
            // 创建URL
            URL url = new URL(imgFile);
            byte[] by = new byte[1024];
            // 创建链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            InputStream is = conn.getInputStream();
            // 将内容放到内存中
            int len = -1;
            while ((len = is.read(by)) != -1) {
                data.write(by, 0, len);
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        return Base64.encodeBase64String(data.toByteArray());
    }

    /**
     * 本地图片转换成base64字符串
     *
     * @param imgFile 图片本地路径
     * @return base64字符串
     * @author szs
     * @date 2020-07-16
     */
    public static String getBase64FromLocal(String imgFile) {
        InputStream in;
        byte[] data = null;

        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);

            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Base64.encodeBase64String(data);
    }

    /**
     * 字符流转bae64
     *
     * @param in InputStream
     * @return base64字符串
     * @author szs
     * @date 2020-05-05
     */
    public static String getBase64FromInputStream(InputStream in) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        byte[] data = null;
        // 读取图片字节数组
        try {
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = in.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new String(Objects.requireNonNull(Base64.encodeBase64(data)));
    }


}
